<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );


/**
 * @package		Joomla
 * @subpackage	JobBoard
 */
class JobBoardTableJA_Employer extends JTable
{
    
    /** @var int Primary key */
   	var $id;
	
	var $user_id;
	
	var $standard_posts_balance;
	
	var $premium_posts_balance;
	
	var $subscription_can_view_resume;
	
	var $subscription_can_premium_post;
	
	var $subscription_can_standard_post;
	
	var $membership_active;
	
	var $credit_balance;
	
	var $begin_date_search_resume;
	
	var $end_date_search_resume;
   
	var $featured;
	
	var $ordering;
	
	var $hiring;

	/**
	* @param database A database connector object
	*/
	function __construct(&$db)
	{
		global $mainframe, $option;
        
        parent::__construct( "#__ja_employers", 'user_id', $db );
	}
	
	/**
	 * Overloaded check function
	 *
	 * @access public
	 * @return boolean
	 * @see JTable::check
	 * @since 1.5
	 */
	function check()
	{
		JFilterOutput::objectHTMLSafe($this,ENT_QUOTES,'');
		return true;
			
	}
	
	function move($movement){
//		var_dump($movement);exit; 	
		$k = $this->_tbl_key;
		if($movement > 0){
		    $query = "SELECT k.user_id, k.ordering FROM ".$this->_tbl." AS k"
		    	." WHERE k.ordering > " . $this->ordering 
		    	. " ORDER BY k.ordering ASC";
		} else if ($movement < 0){
			$query = "SELECT k.user_id, k.ordering FROM ".$this->_tbl." AS k"
		    	." WHERE 0 < k.ordering AND k.ordering < " . $this->ordering
				. " ORDER BY k.ordering DESC"; 
		}
		$this->_db->setQuery($query, 0, 1);

		$row = null;
		$row = $this->_db->loadObject();
		if (isset($row))
		{
			$query = 'UPDATE '. $this->_tbl
			. ' SET ordering = '. (int) $row->ordering
			. ' WHERE '. $this->_tbl_key .' = '. $this->_db->Quote($this->$k)
			;
			$this->_db->setQuery( $query );
            
			if (!$this->_db->query())
			{
				$err = $this->_db->getErrorMsg();
				JError::raiseError( 500, $err );
			}

			$query = 'UPDATE '.$this->_tbl
			. ' SET ordering = '.(int) $this->ordering
			. ' WHERE '.$this->_tbl_key.' = '.$this->_db->Quote($row->$k)
			;
			$this->_db->setQuery( $query );
           
			if (!$this->_db->query())
			{
				$err = $this->_db->getErrorMsg();
				JError::raiseError( 500, $err );
			}

			$this->ordering = $row->ordering;
		}
		else
		{
			$query = 'UPDATE '. $this->_tbl
			. ' SET ordering = '.(int) $this->ordering
			. ' WHERE '. $this->_tbl_key .' = '. $this->_db->Quote($this->$k)
			;
			$this->_db->setQuery( $query );

			if (!$this->_db->query())
			{
				$err = $this->_db->getErrorMsg();
				JError::raiseError( 500, $err );
			}
		}
		return true;
	}	
}